package org.zheng.nio;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zhengqn
 * @Title: 读取文件内容
 * @Description:
 * @date 2020/11/17 15:31
 */
public class NioFileInput {

    public static void main(String[] args) throws Exception{
        //读取文件
        FileInputStream fileInputStream = new FileInputStream("a.txt");
        //获取channel
        FileChannel channel = fileInputStream.getChannel();
        //引入ByteBufer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将文件通过channel读取到buffer中
        int read = -1;
        //如果read的值不为-1，则说明文件中还有其他数据
        while ((read = channel.read(byteBuffer)) != -1){
            //字节数组转换成字符串
            String str = new String(byteBuffer.array(), 0, read,"GBK");
            System.out.println(str);
        }
        fileInputStream.close();
    }
}
